
import { Button, Modal } from 'antd';
import { inject, observer } from "mobx-react";
import { toJS } from 'mobx';
import { WeaDialog, WeaFormItem, WeaSearchGroup, WeaNewScroll, WeaRightMenu } from 'ecCom';
import { WeaSwitch, WeaTableNew } from 'comsMobx';
import CptInventoryPlanForm from '../form/CptInventoryPlanForm';
import CptInventoryImport from '../inventory/CptInventoryImport';

const WeaTable = WeaTableNew.WeaTable;
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@observer
class MyPlanListInfoDialog extends React.Component {
    constructor(props) {
        super(props);
    }
    componentDidMount() {
    }
    componentWillReceiveProps(nextProps) {
    }

    render() {
        const { visible, basicInfo, setVisiable, canEditCpt,sessionkey_plan, plantableStore, inventoryVisible, importVisible,
            inventoryform, inventoryformCondition, doInventory, maintitle, doImport, clearStatus, reflesh } = this.props.contentStore;
        let editButtons = [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@0myfqz@submit`} type="primary" onClick={() => doInventory()} >{getLabel(615, "提交")}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9upwh6@close`} type="ghost" onClick={() => this.closeDialog(1)} >{getLabel(309, "关闭")}</Button>)
        ]
        if(canEditCpt){
            editButtons = [
                (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@li0cmq@close`} type="ghost" onClick={() => this.closeDialog(1)} >{getLabel(309, "关闭")}</Button>)
            ]
        }
        let inventoryButtons = [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@48p327@submit`} type="primary" onClick={() => doImport()} >{getLabel(615, "提交")}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@cs0ppf@close`} type="ghost" onClick={() => this.closeDialog(2)} >{getLabel(309, "关闭")}</Button>)
        ]
        
        let dialogHeight = window.innerHeight;
        let dialogWidth = 1200;
        if (dialogHeight > 768) {
            dialogHeight = 600;
        }else{
            dialogHeight = 500;
            dialogWidth = 1000;
        }

        return (
            <div>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@9i6id9`}
                    title={maintitle}
                    visible={visible}
                    icon="icon-coms-fa"
                    iconBgcolor="#f14a2d"
                    onCancel={() => { this.props.contentStore.handleDialog(false); }}
                    style={{ width: dialogWidth, height: dialogHeight }}
                >
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@vwv9bv`} datas={this.getRightMenu()} onClick={this.onRightMenuClick.bind(this)}  >
                            <div className='cpt-dialog-form'>
                                {this.getFormFields()}
                            </div>
                            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@vkl2bu`}
                                sessionkey={sessionkey_plan}
                                comsWeaTableStore={plantableStore}
                                outerHeight={dialogHeight}
                                hasOrder={true}
                                needScroll={true}
                                onOperatesClick={this.onOperatesClick} />
                    </WeaRightMenu>
                </WeaDialog>

                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@xotnks`}
                    title={getLabel(1506, "盘点") + getLabel(535, "资产")}
                    visible={inventoryVisible}
                    onCancel={() => { setVisiable(false, 1) }}
                    icon='icon-coms-fa'
                    iconBgcolor='#f14a2d'
                    buttons={editButtons}
                >
                    <CptInventoryPlanForm ecId={`${this && this.props && this.props.ecId || ''}_CptInventoryPlanForm@sisoy0`} form={inventoryform} formcondition={inventoryformCondition} />
                </WeaDialog>

                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@gtvk07`}
                    title={getLabel(18596, "导入") + getLabel(1506, "盘点")}
                    visible={importVisible}
                    onCancel={() => { this.closeDialog(2) }}
                    icon='icon-coms-fa'
                    iconBgcolor='#f14a2d'
                    buttons={inventoryButtons}
                    style={{ width: 750, height: 400 }}
                >
                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@lc4vez`} height={"100%"}>
                        <CptInventoryImport ecId={`${this && this.props && this.props.ecId || ''}_CptInventoryImport@5cbt3x`} contentStore={this.props.contentStore} key={reflesh} />
                    </WeaNewScroll >
                </WeaDialog>

            </div>)
    }

    onRightMenuClick(key) {

    }

    closeDialog(param) {
        const { setVisiable, clearStatus, handleDialog, selectid, maintitle } = this.props.contentStore;
        if (param == 1) {
            setVisiable(false, 1);
        } else {
            setVisiable(false, 2);
            clearStatus();
            handleDialog(true, { id: selectid, name: maintitle }, 0);
        }

    }

    getFormFields() {
        const { contentStore } = this.props;
        const { basicInfo, planlistForm } = contentStore;
        const { isFormInit } = planlistForm;
        let group = [];
        //  const formParams = instockForm.getFormParams(); 
        isFormInit && basicInfo.map((c,i) => {
            let items = [];
            c.items.map(fields => {
                let hide = false;
                let hasunderline = false;
                 //只读样式控制
                if (fields.viewAttr == "1" || (fields.conditionType == "BROWSER" && fields.browserConditionParam.viewAttr == "1")) {
                    hasunderline = true 
                }
                if(hasunderline){
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@3pafc5@${fields.domkey[0]}`}
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} underline>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@tzq0u1@${fields.domkey[0]}`} fieldConfig={fields} form={planlistForm} />
                        </WeaFormItem>),
                        colSpan: 1,
                        hide: hide
                    });
                }else{
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@glzsyi@${fields.domkey[0]}`}
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} >
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@m5yee9@${fields.domkey[0]}`} fieldConfig={fields} form={planlistForm} />
                        </WeaFormItem>),
                        colSpan: 1,
                        hide: hide
                    });
                }
                
            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@tf5hmf@${i}`} needTigger={true} title={c.title} col={2} showGroup={c.defaultshow} items={items} />)
        });
        return group;
    }
    editTableDatas(datas) {
        const { contentStore } = this.props;
        contentStore.saveSubListData(datas);
    }

    onOperatesClick = (record, index, operate, flag, argumentString) => {
        const myInventoryStore = this.props.contentStore;
        let _href = operate && operate.href ? operate.href : "";
        let fn = _href.replace("javascript:", "");
        fn = fn.substring(0, fn.indexOf('('));
        let that = this;
        if (fn != "") {
            if ("startInventory" == fn) {
                myInventoryStore.getInventoryForm({ inventoryId: record.id,viewAttr:2 });
                myInventoryStore.setVisiable(true, 1);
            }else if ("viewInventory" == fn) {
                myInventoryStore.getInventoryForm({ inventoryId: record.id,viewAttr:1 });
                myInventoryStore.setVisiable(true, 1);
            }
        }
    }

    getRightMenu() {
        const { setVisiable,canEditCpt } = this.props.contentStore;
        let btns = [];
        if (!canEditCpt) {
            btns.push({
                key: "importMain",
                icon: <i className='icon-coms-Preservation' />,
                content: getLabel(18596, "导入") + getLabel(1506, "盘点"),
                onClick: key => {
                    setVisiable(true, 2);
                }
            });
        }
        return btns;
    }
}

export default MyPlanListInfoDialog;